﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using BOHVote.Common;
using BOHVote.DAL;
using System.Collections;

namespace BOHVote.BLL
{
    public static class ApplyTestClass
    {

        public static DataSet getAllTestResult(string ApplyPeriod, string IDCard, string ApplyName, string GovernID,string UnitID)
        {
            ApplyTest at = new ApplyTest();
            //if (UnitID == "-1")
            //    UnitID = "";
            if (GovernID == "-1")
                GovernID = "";
            if (UnitID == "-1")
                UnitID = "";
            DataSet dstSP = at.getAllTestResult(ApplyPeriod, IDCard, ApplyName, GovernID, UnitID);
            ArrayList arrIndexs = new ArrayList();
            ArrayList arrValue = new ArrayList();
            int i = 8;
            while (i<dstSP.Tables[0].Columns.Count)
            {
                string Cvalue = dstSP.Tables[0].Columns[i].Caption;
                if (Cvalue.IndexOf("_") > 0)
                {
                    arrIndexs.Add(i);
                    string[] str = Cvalue.Split(new char[] { '_'});
                    arrValue.Add(str[0]);
                }
                i = i + 2;
            }

            foreach (DataRow dr in dstSP.Tables[0].Rows)
            {
                if (dr["UnitID"].ToString() != "")
                    dr["UnitID"] = UnitClass.getUnitName(dr["UnitID"].ToString());
                if (dr["GovernID"].ToString() != "")
                    dr["GovernID"] = UnitClass.GetGovernName(dr["GovernID"].ToString());
                for (int j = 0; j<arrIndexs.Count;j++)
                {
                    int a = (int)arrIndexs[j];
                    dr[a] = arrValue[j];
                }
            }
            return dstSP;
        }

        public static DataSet getAllTestType()
        {
            ApplyTest at = new ApplyTest();

            return at.getAllTestType();
        }

        public static bool hasTestResult(string ApplyID, string ApplyPeriod, string TestType)
        {
            int intCount = 0;
            bool returnValue = false;
            ApplyTest at = new ApplyTest();
            intCount = at.TestResultCount(ApplyID, ApplyPeriod, TestType);

            if (intCount > 0)
                returnValue = true;
            else
                returnValue = false;

            return returnValue;
        }

        #region <<  Modfiy TestResult  >>
        public static bool InsertNewTestResult(string ApplyID, string TestType, string testResule, string ApplyPeriodID)
        {
            bool returnValue = false;
            int intValue;

            try {
                ApplyTest at = new ApplyTest();
                intValue = at.InsertNewTestResule(ApplyID, TestType, testResule, ApplyPeriodID);

                if (intValue == -1)
                    returnValue = false;
                else
                    returnValue = true;
            }
            catch (Exception ex)
            {
                returnValue = false;
            }

            return returnValue;
        }

        public static bool UpdateTestResult(string ApplyID, string TestType, string testResule, string ApplyPeriodID)
        {
            bool returnValue = false;
            int intValue;

            try
            {
                ApplyTest at = new ApplyTest();
                intValue = at.UpdateTestResult(ApplyID, TestType, testResule, ApplyPeriodID);

                if (intValue == -1)
                    returnValue = false;
                else
                    returnValue = true;
            }
            catch (Exception ex)
            {
                returnValue = false;
            }

            return returnValue;
        }

        #endregion

        #region << Modfiy  TestType>>
        public static bool InsertTestType(string TestTypeName, string status,string ApplyPeriod)
        {
            bool returnValue = false;
            int intValue;
            try
            {
                int intNewTestTypeID;
                ApplyTest at = new ApplyTest();
                intNewTestTypeID = at.InsertNewTestType(TestTypeName, status);
                DataSet dst = at.getALLApplyIDInResult(ApplyPeriod);
                foreach (DataRow dr in dst.Tables[0].Rows)
                {
                    at.InsertNewTestResule(dr["ApplyID"].ToString().Trim(), intNewTestTypeID.ToString(), "0", ApplyPeriod);
                }
                intValue = 0;
            }
            catch (Exception ex)
            {
                intValue = -1;
            }

            if (intValue == -1)
                returnValue = false;
            else
                returnValue = true;

            return returnValue;
        }

        public static bool UpdateTestType(string TestTypeName, string status, string TestTypeID)
        {
            bool returnValue = false;
            int intValue;

            ApplyTest at = new ApplyTest();
            intValue = at.UpdateNewTestType(TestTypeName, status, TestTypeID);
            if (intValue == -1)
                returnValue = false;
            else
                returnValue = true;

            return returnValue;
        }

        public static bool DeleteTestType(string TestTypeID)
        {
            bool returnValue = false;
            int intValue;

            ApplyTest at = new ApplyTest();
            intValue = at.DeleteTestType(TestTypeID);
            if (intValue == -1)
                returnValue = false;
            else
                returnValue = true;

            return returnValue;
        }

        #endregion

    }
}
